package com.huiyou.adminapi.controller;

import com.huiyou.adminapi.common.model.PseudoCode;
import com.huiyou.adminapi.service.PseudoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import lombok.Data;

@RestController
@RequestMapping("/pseudo")
public class PseudoController {

    private PseudoService pseudoService;

    @Autowired
    public PseudoController(PseudoService pseudoService) {
        this.pseudoService = pseudoService;
    }

    @GetMapping
    public Page<PseudoCode> findAll(String goodsName, String goodsBatch,
                                    @PageableDefault Pageable pageable) {
        return this.pseudoService.findAll(goodsName, goodsBatch, pageable);
    }

    @GetMapping("/{securityId}")
    public PseudoCode findBySecurityId(@PathVariable("securityId") String securityId,
                                       @RequestParam String address) {
        System.out.println(address);
        return this.pseudoService.findBySecurityId(securityId, address);
    }

    @PostMapping("/upload")
    public void singleFileUpload(@RequestBody GoodsBind goodsBind) {

        this.pseudoService.save(goodsBind.getQrCodes(), goodsBind.getPseudoCode());
    }

    @DeleteMapping("/{goodsName}/{goodsBatch}")
    public void deleteByGoodNameAndGoodBatch(@PathVariable String goodsName, @PathVariable String goodsBatch) {
        this.pseudoService.deleteByGoodsNameAndGoodsBatch(goodsName, goodsBatch);
    }

    @GetMapping("/findBatchAndName")
    public List<PseudoCode> findByGoodsBatchAndGoodsName(String batch, String name, @PageableDefault(size = 10) Pageable pageable) {

        return pseudoService.findByGoodsBatchAndGoodsName(batch, name, pageable);
    }

    @DeleteMapping("{id}")
    public void deleteById(@PathVariable("id") String id){
        pseudoService.deleteById(id);
    }

    @Data
    static class GoodsBind {
        PseudoCode pseudoCode;
        List<String> qrCodes;
    }

}
